//
//  WantCommendViewController.m
//  GoodWayClient
//
//  Created by HuiDong on 15/12/23.
//  Copyright © 2015年 HuiDong. All rights reserved.
//

#import "WantCommendViewController.h"
#import "AFNetworking.h"
#import "Constant.h"
#import "AppDelegate.h"
#import "RecommendUser.h"

@interface WantCommendViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *iphone;
@property (weak, nonatomic) IBOutlet UITextField *descrip;
@end

@implementation WantCommendViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.name.delegate = self;
    self.iphone.delegate = self;
    self.descrip.delegate = self;
}

- (IBAction)submit:(id)sender {
    //网络请求
    AFHTTPRequestOperationManager *request = [AFHTTPRequestOperationManager manager];
    request.responseSerializer = [AFJSONResponseSerializer serializer];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    dic[@"token"] = tempAppDelegate.token;
    NSError *error;
    NSDictionary *dic1 = @{@"Name":self.name.text,@"PhoneNum":self.iphone.text,@"ReContent":self.descrip.text};
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic1 options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    dic[@"model"] = jsonString;
    
    [request POST:[NSString stringWithFormat:@"%@%@",url2,@"RecommendUser"] parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if ([[NSString stringWithFormat:@"%@",dic[@"Status"]] isEqualToString:@"0"]) {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"提交成功!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
            [self performSelector:@selector(back) withObject:nil afterDelay:1.0f];
        }else
        {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:dic[@"Message"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"系统网络申请错误!");
    }];
}

//UITextField实现的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}
@end
